%% Working with daily time series in IRIS
% by Jaromir Benes
%
% IRIS support for daily time series differs from the way other frequencies
% (annual, biannual, quarterly, bimonthly, monthly) are handled. The dates
% are created as the standard Matlab serial date numbers (and hence are
% very different than IRIS serial date numbers). The Matlab serial date
% numbers are plain integers (e.g. 730486 for 1/1/2000), and IRIS
% interprets them as dates with indeterminate frequency. You can, however,
% request that they be treated as daily dates/daily time series in certain
% circumstances.

%% Clear Workspace

clear;
close all;
clc;
%#ok<*NOPTS>
irisrequired 20141001;

%% Loading Daily Series from CSV Data File
%
% You must use the option `'freq='` set to `'daily'` or `365` (these two
% are equivalent). On top of that, you will also usually need to specify
% the date format.

Dd = dbload('CDS.csv','dateFormat=','$M/D/YYYY')

%% Display Daily Tseries
%
% By default, daily tseries are displayed one month at a row.

Dd.CCROA1E5Index
Dd.CHUN1E5Index

%% Plotting daily series
%
% Using the `plot` function with a daily series will produce a graph whose
% time axis will be just Matlab's serial date numbers. To translate these
% into human-readable dates, use Matlab's `datetick` function.

figure();
plot(Dd.CCROA1E5Index,'marker','.');
grid('on');
title(comment(Dd.CCROA1E5Index));

%% Interpolating missing observations
%
% You can use the `interp` function to interpolate NaN observations found
% within a specified range; if you enter Inf for the range, the whole time
% series is searched for NaNs. The function is based on the standard Matlab
% `interp1` function, and can be called with the same range of methods (the
% 'method' option). The default method is 'cubic', i.e. shape-preserving
% piecewise cubic interpolation.

i1 = interp(Dd.CCROA1E5Index,Inf);
i2 = interp(Dd.CCROA1E5Index,Inf,'method=','spline');
i3 = interp(Dd.CCROA1E5Index,Inf,'method=','nearest');

figure();

plot(dd(2009,2,1):dd(2009,2,'end'), ...
    [i1,i2,i3,Dd.CCROA1E5Index],'marker','.')

grid on;
legend('Piecewise Cubic Interp','Cubic Spline', ...
    'Nearest Neighbour','Data','location','best');
title('Different Methods of Interpolating Missing Observations');

%% Converting daily series to lower frequencies
%
% The IRIS function `convert` can be used with daily time series in a usual
% way. The option `'missing'` (available only with daily time series)
% specifies a value to use for missing observations (such as NaN, which is
% the default value, or 0). Alternatively, it can be set to 'last' in which
% case the last available observation before the missing one is used. Other
% options, i.e. `'method'` and `'ignorenan'`, work the same way as with
% other frequencies.

% Convert to weekly series.
w = convert(Dd.CCROA1E5Index,52)

% Convert to monthly series.
m1 = convert(Dd.CCROA1E5Index,12)

% Create monthly series filling in the gaps with the last available
% observations.
m2 = convert(Dd.CCROA1E5Index,12,Inf,'missing','last')

% Create quarterly series.
q = convert(Dd.CCROA1E5Index,4)

figure();
hold all;
plot(q,'marker','.');
plot(w,'marker','.');
plot([m1,m2],'marker','.');
grid('on');
legend('Quarterly','Weekly','Monthly Missing=NaN','Monthly Missing=last');
title('Aggregated Series');

%% Converting a whole database
%
% As in other similar situations, you can use the `dbbatch` command to
% batch-process all entries in a database at once.

Dm = dbfun(@(x) convert(x,12,Inf,'missing=','last'),Dd);
Dq = dbfun(@(x) convert(x,4,Inf,'missing=','last'),Dd);

% Display the contents of the monthly and quarterly databases.
Dm
Dq

%% Help on IRIS functions used in this m-file
%
% Use either `help` to display help in the command window, or `idoc`
% to display help in an HTML browser window.
%
%    help data/dbload
%    help data/dbfun
%    help tseries/convert
%    help tseries/subsref
%    help tseries/daily
%    help tseries/interp
